/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pio.action.coordinador;

import DBMS.DataBaseManager;
import clases.Aspirante;
import clases.Institucion;
import clases.InstitucionAspirante;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

/**
 *
 * @author tamerdark
 */
public class Consultar extends DispatchAction {

    public ActionForward find(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        Aspirante a = (Aspirante) form;
        List instituciones = Comun.fillInstituciones(request);
        DataBaseManager dbms = DataBaseManager.getInstance();
        ArrayList <InstitucionAspirante> l = dbms.consultarAspirantes(a);
        if (l.isEmpty()) {
            a.setMensaje("No se encontraron resultados");
            return mapping.findForward("failure");
        } else {
//            request.setAttribute("aspirantes", l);
            ArrayList ll = new ArrayList();
            for (InstitucionAspirante i : l) {
                if (buscar(i.getId(), instituciones)){
                    ll.add(i);
                }
            }
            request.setAttribute("aspirantes", ll);
            return mapping.findForward("success");
        }
    }

    public ActionForward populate(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        Comun.fillInstituciones(request);
        return mapping.findForward("failure");
    }

    private boolean buscar(int id, List <Institucion> instituciones) {
        for (Institucion i : instituciones){
            if (i.getId()==id){
                return true;
            }
        }
        return false;
    }
}
